package net.tngou.iask.service;

import java.sql.Date;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.math.NumberUtils;

import net.tngou.iask.dao.CureDao;
import net.tngou.iask.dao.DiseaseDao;
import net.tngou.iask.jdbc.PageUtil;
import net.tngou.iask.pojo.Access;
import net.tngou.iask.pojo.Cure;
import net.tngou.iask.pojo.Department;
import net.tngou.iask.pojo.Disease;
import net.tngou.iask.pojo.Place;
import net.tngou.iask.pojo.Survey;
import net.tngou.iask.pojo.Treatment;
import net.tngou.iask.pojo.User;
import net.tngou.iask.util.DateUtil;
import net.tngou.iask.util.StringUtil;

/**
 * 治疗
 * @author tngou@tngou.net
 *
 */
public class CureService extends BaseService {

	
	/**
	 * 保存治疗
	 * @param cure 治疗
	 * @param disease 疾病
	 * @param place 身体位置
	 * @param treatment 治疗方式
	 * @param department 科室
	 * @param user 用户
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public long save(Cure cure,Disease disease,String[] place,String[] treatment,String[] department,String[] user) {
		
		if(StringUtil.isEmpty(cure.getDescription()))cure.setDescription(disease.getName());//
		if(StringUtil.isEmpty(cure.getNumber())){
			//实体（id）+病人(id)+疾病(id)+递增			
		  String number= cure.getMain()+""+cure.getPatient()+""+cure.getDisease()+""
		  +cure.totalCount("main="+cure.getMain()+" AND patient="+cure.getPatient());
		  cure.setNumber(number);   
		}
		if(cure.getTdate()==null)
			cure.setTdate(new Date(new java.util.Date().getTime()));	
		if(cure.getStartdate()==null)
			cure.setStartdate(new Date(new java.util.Date().getTime()));		
		if(cure.getEnddate()==null)	
			cure.setEnddate(DateUtil.toSqlDate(DateUtil.toLocalDateTime(cure.getStartdate()).plusDays(disease.getPeriod())));			
		if(cure.getCycle()==0)
			cure.setCycle(disease.getCycle());	
		if(cure.getLfdate()==null)
			cure.setLfdate(new Date(new java.util.Date().getTime()));
		if(cure.getNfdate()==null)
			cure.setNfdate(DateUtil.toSqlDate(DateUtil.toLocalDateTime(cure.getLfdate()).plusDays(disease.getCycle())));		
		Integer places[]=null;
		Integer treatments[]=null;
		Integer departments[]=null;
		Long users[]=null;
		SurveyService service = new SurveyService();
		List<Survey> surveys = service.getSurvey(disease.getId());
		if(surveys!=null&&surveys.size()>0)
			surveys=(List<Survey>) service.getList("main="+cure.getMain()+" AND type=2", Survey.class);
		if(surveys!=null&&surveys.size()>0)
			cure.setSurvey(surveys.get(0).getId());
		if(cure.getSurvey()==0)cure.setSurvey(2);
		long id = cure.save();
		if(id==0)return id;
		
		DiseaseDao dao = new DiseaseDao();
		if(place!=null)//身体位置
		{
			places= _change(place);
		}else
		{
			List<Integer> list =dao.getPlaceId(disease.getId());
			places=list.toArray(new Integer[list.size()]);
		}
		if(treatment!=null)//治疗方法
		{
			treatments= _change(treatment);
		}else
		{
			List<Integer> list =dao.getTreatmentId(disease.getId());
			treatments=list.toArray(new Integer[list.size()]);
		}
		if(department!=null)//科室
		{		
			departments= _change(department);		
		}else
		{
			List<Integer> list =dao.getDepartmentId(disease.getId());
			departments=list.toArray(new Integer[list.size()]);
		}
		if(user!=null)//用户
		{		
			List<Long> list = new ArrayList<>();
			for (String s : user) {
				if(NumberUtils.isNumber(s))
					list.add(Long.parseLong(s));
			}		
			users= list.toArray(new Long[list.size()]);;		
		}
		CureDao cureDao = new CureDao();
		cureDao.update(id, places, treatments, departments, users);
		return id;
		
	}
	
	
	/**
	 * 更新
	 * @param id 治疗ID
	 * @param place
	 * @param treatment
	 * @param department
	 * @param user
	 */
	public void update(long id,String[] place,String[] treatment,String[] department,String[] user) {
		Integer places[]=null;
		Integer treatments[]=null;
		Integer departments[]=null;
		Long users[]=null;
		if(place!=null)//身体位置
			places= _change(place);
		
		if(treatment!=null)//治疗方法
			treatments= _change(treatment);
		
		if(department!=null)//科室
			departments= _change(department);		
		
		if(user!=null)//用户
		{		
			List<Long> list = new ArrayList<>();
			for (String s : user) {
				if(NumberUtils.isNumber(s))
					list.add(Long.parseLong(s));
			}		
			users= list.toArray(new Long[list.size()]);;		
		}
		CureDao cureDao = new CureDao();
		cureDao.update(id, places, treatments, departments, users);
	}
	
	/**
	 * 取得对应实体用户
	 * @param main 实体
	 * @param patient 病人ID
	 * @return
	 */
	public List<Map<String, Object>> getList(long main,long patient)  {
		CureDao dao = new CureDao();
		return dao.getList(main, patient);		
	}
	
	/**
	 * 取得实体中的治疗
	 * @param page
	 * @param size
	 * @param main 实体
	 * @param keyword 关键词
	 * @return
	 */
	public PageUtil getPage(int page,int size,long main,String keyword) {
		CureDao dao = new CureDao();
		return dao.getPage(page, size, main, keyword);
	}
	public PageUtil getPage(int page,int size,long main,int type,String keyword) {
		CureDao dao = new CureDao();
		return dao.getPage(page, size, main,type, keyword);
	}
	/**
	 * 取得治疗 疾病对应信息
	 * @param page
	 * @param size
	 * @param main
	 * @param disease
	 * @return
	 */
	public PageUtil getPage(int page,int size,long main,long disease) {
		CureDao dao = new CureDao();
		return dao.getPage(page, size, main,disease);
	}
	
	/**
	 * 取得疾病对应的身体位置
	 * @param cure 治疗ID
	 * @return
	 */
	public List<Place> getPlace(long cure) {
		CureDao dao = new CureDao();
		return dao.getPlace(cure);	
	}
	
	/**
	 * 取得疾病对应的就诊科室
	 * @param disease  疾病ID
	 * @return
	 */
	public List<Department> getDepartment(long cure) {
		CureDao dao = new CureDao();
		return dao.getDepartment(cure);	
	}
	
	/**
	 * 取得疾病对应的治疗方式
	 * @param cure  疾病ID
	 * @return
	 */
	public List<Treatment> getTreatment(long cure) {
		CureDao dao = new CureDao();
		return dao.getTreatment(cure);		
	}
	/**
	 * 取得治疗对应的主管医生
	 * @param cure  治疗ID
	 * @return
	 */
	public List<User> getUser(long cure) {
		CureDao dao = new CureDao();
		return dao.getUser(cure);	
	}
	
	
	/**
	 * 保存随访
	 * @param cure 治疗
	 * @param survey 随访问卷
	 * @param type 随访类型
	 * @return
	 */
	public long saveAccess(Cure cure,long survey) {
		Access access = new Access();
		access.setPatient(cure.getPatient());
		access.setType(2);//随访
		access.setCure(cure.getId());
		access.setMain(cure.getMain());
		access.setSurvey(survey);
		long id = access.save();
		if(id>0)
		{
			LocalDateTime ltime = LocalDateTime.now();
			cure.setLfdate(DateUtil.toSqlDate(ltime));//最近随访时间
			LocalDateTime ftime = ltime.plusDays(cure.getCycle());
			cure.setNfdate(DateUtil.toSqlDate(ftime));
			cure.update();
		}
		return id;
		
	}
	
	/**
	 * 取得需要执行的数据
	 * @param date
	 * @return
	 */
	public List<Cure> getFollowCure(int date) {
		CureDao dao = new CureDao();
		return dao.getFollowCure(date);	
	}
	
	private Integer[] _change(String[]  sts) {
		List<Integer> list = new ArrayList<>();
		for (String s : sts) {
			if(NumberUtils.isNumber(s))
				list.add(Integer.parseInt(s));
		}
		return list.toArray(new Integer[list.size()]);
	}
}
